﻿using System.Data;
using System.Linq;
using Ninject;
using SDM.ExamPlanner.DALC.Interfaces;
using SDM.ExamPlanner.DTO;

namespace SDM.ExamPlanner.DALC
{
    public class DataRepository<T> : IRepository<T> where T : class
    {
        private readonly DataContext _context = Ninjector.Kernel.Get<DataContext>();

        public IQueryable<T> Find()
        {
            return _context.Set<T>();
        }

        public void RegisterNew(T entity)
        {
            _context.Set<T>().Add(entity);
        }

        public void RegisterUpdated(T entity)
        {
            if (_context.Entry(entity).State == EntityState.Detached)
            {
                _context.Set<T>().Attach(entity);
            }
            _context.Entry(entity).State = EntityState.Modified;
        }

        public void RegisterDeleted(T entity)
        {
            _context.Set<T>().Remove(entity);
        }

        public int SaveChanges()
        {
            return _context.SaveChanges();
        }
    }
}
